package com.fq.binderconnectpool;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Scroller;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textView;

    private static final String TAG = MainActivity.class.getCanonicalName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textShow);
        textView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });

    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    textView.setText((String) msg.obj);
                    break;
                case 1:
                    textView.setText((String) msg.obj);
                    break;
                default:
                    break;
            }
        }
    };

    public void encodeClick(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                doEncrypt();
            }
        }).start();
    }

    public void sumClick(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                doAddition();
            }
        }).start();
    }

    /**
     * 调用加密服务
     */
    private void doEncrypt() {
        BinderPool binderPool = BinderPool.getInstance(getApplicationContext());
        IBinder securityBinder = binderPool.queryBinder(BinderPool.BINDER_SECURITY);
        ISecurityCenter securityCenter = SecurityImpl.asInterface(securityBinder);
        String msg = "Hello, I am !";
        try {
            String encryptMsg = securityCenter.encrypt(msg);
            Log.e(TAG, "加密信息: " + encryptMsg);
            String decryptMsg = securityCenter.decrypt(encryptMsg);
            Log.e(TAG, "解密信息: " + decryptMsg);
            Message hm = new Message();
            hm.what = 0;
            hm.obj = "加密信息：" + encryptMsg + "\n" + "解密信息：" + decryptMsg;
            mHandler.sendMessage(hm);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void doAddition() {
        BinderPool binderPool = BinderPool.getInstance(getApplicationContext());
        IBinder computeBinder = binderPool.queryBinder(BinderPool.BINDER_COMPUTE);
        ICompute mICompute = ComputeImpl.asInterface(computeBinder);
        try {
            int result = mICompute.add(12, 12);
            Log.e(TAG, "12 + 12 = " + result);

            Message hm = new Message();
            hm.what = 1;
            hm.obj = "12 + 12 = " + result + "";
            mHandler.sendMessage(hm);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
